import publicForm from "@/components/public/up-form.vue"; //弹框form
import publicForm1 from "@/components/public/el-form.vue"; //弹框form

import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    getBaseDepartmentUser, //提交审核人
    getMonthlyReportByFid, //详情
    saveMonthlyreportManagement, //保存修改
    CheckMonthlyreportManagement, //审核
    getMonths, //根据总结日期得到月次
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "projectId",
        "status",
        "statusNum",
        "dataFid",
        "currentRow",
        "projectName",
        "fCompanyid",
        "stationType",
        "title"
    ],
    data() {
        return {
            visible: "",
            // title: "新增",
            isLock: false,
            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            formData: {
                fId: "", //传此表示修改
                userId: "", //当前登录人id
                userName: "", //当前登录人名称
                fProjectname: "", //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
                fProjectid: "", //当前登录人项目ID
                fCompanyname: "", //当前登录人标段名称
                fCompanyid: "", //当前登录人标段ID
                fConstructionpoject: "", //工程建设项目
                fConstructionpojectid: "", //工程建设项目id
                fUnit: "", //编制单位
                fUnitid: "", //编制单位id
                fAffiliationyear: "", //隶属年度
                fAffiliationmonth: this.common.getMonth(0), //隶属月份
                fMonthreport: "", //月报其次
                fWeaveexplain: "", //编制说明
                fWeaveid: "", //编制人id
                fWeavename: "", //编制人姓名
                fWeavedates: this.common.getDay(0), //编制日期
                fMonthfileurl: "",
                fSubcheckid: "", //提交审核人id
                fSubcheckname: "", //提交审核人姓名
                recordfiles: [],
                fType: null
            },
            formDatafileList: [], //工作计划资料
            fCcpeopleData: [], //计划传阅人
            fFinishccpeopleData: [], //完成传阅人
            monthExamine: formJs.monthExamine, //计划审核
            monthExamineData: {
                fId: "", //修改与保存都必须传
                fCheckstatus: "", //审核结果(内容详实,审核通过。)(内容不实，重新上报。)  如审核未通过 打回上一级 状态为”已打回“ 已打回状态下只能删除不能修改
                fCheckdescription: "", //审核说明
                fCheckid: "", //审核人id
                fCheckname: "", //审核人姓名
                fCheckdates: this.common.getDay(0), //审核时间
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名

            }, //审核
            btnShow: true,
            FinishfileList: [], //工作计划资料
            fFinishccpeople: [], //计划传阅人
            currentStatus: null,
            fType: null,
            startDates1: null,
            year: null,
            times: null, //开工时间
            erjititle:null
        };
    },
    components: {
        publicForm,
        publicForm1
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        stationType: {
            handler(newVal) {
                if (newVal) {
                   
                    this.fType = newVal;
                    if(newVal==1){
                        this.erjititle="试验室月报审核"

                    }else if(newVal==0){
                        this.erjititle="监理月报审核"

                    }
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        projectId: {
            handler(newVal) {
                if (newVal) {
                    this.getPerson(newVal);
                }
            },
            immediate: true,
        },
        status: {
            handler(newVal) {
                if (newVal) {
                    this.btnStatus = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        dataFid: {
            handler(newVal) {
                if (newVal) {
                    this.currntData = newVal;
                    if (this.statusNum != 1) {
                        this.formData = this.$options.data.call(this).formData;
                        var data = {
                            fId: newVal.fId,
                        }
                        this.getDetail(data);
                    }
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    // this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        this.btnShow = true
        if (this.statusNum == 1) {
            this.formData = this.$options.data.call(this).formData;
            this.getDetail({
                fId: '',
                fCompanyid: this.fCompanyid,

            });
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[4].disabled = true;
            this.formObj[8].uploadSelect = true;
        } else if (this.statusNum == 2) { //编辑
            //编辑
            this.formData = this.$options.data.call(this).formData;
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[4].disabled = true;
            this.formObj[8].uploadSelect = true;
        } else if (this.statusNum == 4) { //审核
            this.formObj.map((item) => {
                item.disabled = true;
            });

            this.formObj[8].uploadSelect = false;
            this.monthExamineData.fCheckid = this.userInfo.userId; //审核人id
            this.monthExamineData.fCheckname = this.userInfo.realName; //审核人姓名
            this.monthExamine.map((item) => {
                item.disabled = false;
            });
            this.monthExamine[2].disabled = true;

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[8].uploadSelect = false;
            this.monthExamine.map((item) => {
                item.disabled = true;
            });

        }
        this.getHeight();
    },
    methods: {
        getMonths(data) {
            getMonths({
                fCompanyid: this.userInfo.companyId,
                startDates: data
            }).then(res => {
                if (res.errcode == 'ok') {
                    this.formData.fMonthreport = res.msg
                }
            })
        },

        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        // fCcpeopleData计划传阅人
        selectChange(data, a, b) {
            console.log(111, data, a, b);
            if (b == "fSubcheckname") {
                this.formData.fSubcheckid = data.value;
                this.formData.fSubcheckname = data.label;
            } else if (b == "fAffiliationmonth") {
                var month = data.length == 3 ? data.slice(0, 1) : data.slice(0, 2)
                console.log(month, data);
                var time = this.formData.fAffiliationyear + '-' + month + '-' + '01'
                this.getMonths(
                    time
                )
            } else if (b == 'fAffiliationyear') {

                this.formData.fAffiliationmonth = ""
                if (data == this.year) { //往年
                    var month1 = this.times.slice(5, 6) == 0 ? this.times.slice(6, 7) : this.times.slice(5, 7)
                    this.formObj[3].options = []
                    for (var i = month1; i <= 12; i++) {
                        this.formObj[3].options.push({
                            label: i + '月份',
                            value: i + '月份',
                        })
                    }
                } else {
                    var month2 = this.common.getMonths(0)
                    this.formObj[3].options = []
                    for (var i1 = 1; i1 <= month2; i1++) {
                        console.log(i1);
                        this.formObj[3].options.push({
                            label: i1 + '月份',
                            value: i1 + '月份',
                        })

                    }
                }
            }
        },
        edit(status) {
            if (status == 1) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 2) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 4) {
                this.monthExamineData = this.$options.data.call(this).monthExamineData;
            }
        },
        getDetail(data) {
            getMonthlyReportByFid(data).then((res) => {
                var data = res.data[0];
                this.dataInfo = data.fId
                if (data.fCheckdates != null && data.fCheckdates != '') {
                    this.monthExamineData = res.data[0];
                } else {

                    this.monthExamineData.fCheckid = this.userInfo.userId; //审核人id
                    this.monthExamineData.fCheckname = this.userInfo.realName; //审核人姓名
                    this.monthExamineData.userId = this.userInfo.userId; //当前登录人id
                    this.monthExamineData.userName = this.userInfo.realName; //当前登录人名称
                    this.monthExamineData.fId = this.dataInfo
                    this.monthExamineData.fCheckdates = this.common.getDay(0)
                }
                if (data.fId != '') {
                    this.formData = res.data[0];
                    this.formDatafileList = res.data[0].fMonthfileurllist
                    this.formData.recordfiles = []
                } else {
                    this.formData.fConstructionpoject = this.userInfo.companyName //工程建设项目
                    this.formData.fConstructionpojectid = this.userInfo.companyId //工程建设项目id
                    this.formData.fMonthfileurl = data.fMonthfileurl
                    //新增
                    this.formData.fMonthreport = data.fMonthreport
                    this.formData.fWeaveid = this.userInfo.userId; //编制人id
                    this.formData.fWeavename = this.userInfo.realName; //编制人
                    this.formData.fAffiliationyear = this.common.getYear(0); //隶属年度
                    // this.formData.fAffiliationyear = data.startDates.slice(0, 4)
                    this.formData.fAffiliationmonth = this.common.getMonth(0); //隶属月份
                    this.formData.fWeavedates = this.common.getDay(0); //编制人
                    this.formData.recordfiles = []
                }
                var month2 = this.common.getMonths(0)


                this.formObj[3].options = []
                for (var i1 = 1; i1 <= month2; i1++) {

                    this.formObj[3].options.push({
                        label: i1 + '月份',
                        value: i1 + '月份',
                    })

                }


                this.year = data.startDates.slice(0, 4)
                this.times = data.startDates; //开工时间


                let curDate = (new Date()).getTime();
                let three = this.getDaysBetween(new Date(data.startDates), new Date(this.common.getDay(0))) * 24 * 3600 * 1000;
                this.startDates1 = curDate - three;


                // var month = data.startDates.slice(5, 6) == 0 ? data.startDates.slice(6, 7) : data.startDates.slice(5, 7)
                // this.formObj[3].options = []
                // for (var i = month; i <= this.common.getMonths(0); i++) {
                //     this.formObj[3].options.push({
                //         label: i + '月份',
                //         value: i + '月份',
                //     })

                // }
            });
        },
        getDaysBetween(date1, date2) {
            const ONE_DAY = 1000 * 60 * 60 * 24; // 一天的毫秒数
            const date1Time = date1.getTime(); // 获取时间戳
            const date2Time = date2.getTime();
            const difference = Math.abs(date1Time - date2Time); // 获取时间差
            return Math.round(difference / ONE_DAY); // 两个日期之间的天数
        },
        // 刪除文件
        delteFile(file) {
            var arr = [];
            var obj = [];
            arr = this.formDatafileList;
            obj = this.formData;
            for (let index = 0; index < arr.length; index++) {
                if (file.uid == arr[index].uid) {
                    if (arr[index].atta == undefined) {
                        obj.recordfiles.push({
                            fId: arr[index].fId,
                        });
                    } else {
                        obj.recordfiles.push({
                            fId: arr[index].atta.fId,
                        });
                    }
                    arr.splice(index, 1);
                }
            }
        },
        yj_saveForm() {
            if (this.statusNum == 1) { //新增
                this.formData.userId = this.userInfo.userId;
                this.formData.userName = this.userInfo.realName;
                this.formData.fProjectname = this.userInfo.projectName //工程建设项目
                this.formData.fProjectid = this.userInfo.projectId //工程建设项目id
                this.formData.fCompanyname = this.userInfo.companyFullName; //当前登录人标段名称
                this.formData.fCompanyid = this.userInfo.companyId; //当前登录人标段ID

                if (this.$refs.publicForm.submitForm()) {
                    if (this.formData.fMonthreport == '第0期') {
                        this.$message.warning("本机构未录入开工时间")
                    } else {
                        this.saveForm(this.formData);
                    }
                } else {
                    console.log("error");
                }
            } else if (this.statusNum == 2) { //编辑
                if (this.$refs.publicForm.submitForm()) {
                    this.saveForm(this.formData);
                } else {
                    console.log("error");
                }
            } else if (this.statusNum == 4) { //审核
                //审核
                this.monthExamineData.fCheckid = this.userInfo.userId; //审核人id
                this.monthExamineData.fCheckname = this.userInfo.realName; //审核人姓名
                this.monthExamineData.userId = this.userInfo.userId; //当前登录人id
                this.monthExamineData.userName = this.userInfo.realName; //当前登录人名称
                this.monthExamineData.fId = this.dataInfo
                if (this.$refs.monthExamine.submitForm()) {
                    this.yj_examine(this.monthExamineData);
                } else {
                    console.log("error");
                }
            }
        },
        // 保存
        saveForm(data) {
            this.isLock = true
            this.formData.fType = this.fType;
            saveMonthlyreportManagement(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.isLock = false
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
        // 审核
        yj_examine(data) {
            // 计划审核
            this.isLock = true
            CheckMonthlyreportManagement(data).then((res) => {
                if (res.errcode == 0) {
                    this.isLock = false
                    this.visible = false;
                    this.$emit("refreshList");
                } else {
                    this.isLock = false
                }
            });
        },
        getPerson(data) {
            var obj = {
                fCompanyid: data,
            };
            getBaseDepartmentUser(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var arr = res.data.childList[0].personList;
                        var personList = [];
                        arr.map((item) => {
                            personList.push({
                                label: item.fRealname,
                                value: item.fUserid,
                            });
                        });
                        this.formObj[9].options = personList;

                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },

    },
};